package com.ruoyi.leif.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.messages.AssistantMessage;
import java.util.Map;

@RestController
@RequestMapping("/ai")
public class DashScopeDemoController {

    @Autowired(required = false)
    private ChatClient chatClient;

    @PostMapping("/chat")
    public String chat(@RequestBody Map<String, String> body) {
        String userInput = body.getOrDefault("input", "你好");
        if (chatClient == null) {
            return "DashScope ChatClient 未注入，请检查依赖和配置。";
        }
        // 构建用户消息
        Prompt prompt = new Prompt(userInput);
        // 发送消息并获取回复
        ChatClient.ChatClientPromptRequestSpec response = chatClient.prompt(prompt);
        if (response instanceof AssistantMessage) {
            return ((AssistantMessage) response).getContent();
        }
        return response.call().content();
    }

    @GetMapping("/ping")
    public String ping() {
        return "DashScope spring-ai 演示接口已就绪";
    }
} 